﻿using System;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;

using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using EZCRS.HCMS.Bll.AIR;

using EZCRS.HCMS.Entity;
using EZCRS.HCMS.Bll;
using EZCRS.HCMS.Bll.baseinfo;
using EZCRS.HCMS.Web;

namespace EZCRS.HCMS.Web.manage.AIR
{
    public partial class room : ManageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (base.BasHotelId == 0)
                Response.Write(CFun.AlertMeg(Resources.Site.MustHotel, "back", ""));

           
            if (!IsPostBack)
            {
                CheckModule(69);

                CFun.SetListControl(ddlStatus, Bllbase_info.getbaseinfo(baseinfotype.房间状态, CFun.CurrentLanguage()), "");
                CFun.SetListControl(cblWeek, HotelBaseInfo.Weebly(), "");

                foreach (ListItem li in cblWeek.Items) li.Selected = true;

                BindData();

                startdate.Attributes.Add("onclick", "event.cancelBubble=true;showCalendarTA('startdate',false,'startdate','startdate',null);");
                startdate.Attributes.Add("onblur", "if(!chkdate(this.value))this.value='yyyy-MM-dd';");

                enddate.Attributes.Add("onclick", "event.cancelBubble=true;showCalendarTA('enddate',false,'enddate','enddate',null);");
                enddate.Attributes.Add("onblur", "if(!chkdate(this.value))this.value='yyyy-MM-dd';");
            }
        }

        private void BindData()
        {
            Bllh_roomcode bl = new Bllh_roomcode();

            string strLan = base.CurrentLanguage;

            if (base.CurrentLanguage == "zh-CN")
                strLan = "0";
            else strLan = "1";

            List<HCRS_RoomCode> lst = bl.getitem("pid = " + base.BasPropertyId + " AND avaliable = 1");

            if (lst != null)
            {
                int i = 0;
                foreach (HCRS_RoomCode room in lst)
                {
                    cblRoom.Items.Add(new ListItem(room.roomname, room.roomcode));

                    cblRoom.Items[i].Attributes["v"] = room.roomcode;
                    cblRoom.Items[i].Attributes["n"] = room.roomname;

                    i++;
                }

                
            }


            BllHotelUtil hotel = new BllHotelUtil();

            DataSet ds = hotel.GetHotelRateList(base.BasPropertyId, 1, 1, 99999999);

            GridView1.DataSource = ds;
            GridView1.DataBind();

        }


        /// <summary>
        /// 保存:
        /// 提取数据库数据，并用数据转换器的LOAD方法装载原有数据
        /// 使用CREATE方法更新数据转换器中的临时数据，再进行序列化操作，
        /// 放入HOTEL_AIR对象中，
        /// 调用BLL的对象保存方法。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnsave_Click(object sender, EventArgs e)
        {

            DateTime dtStart, dtEnd;

            if (!DateTime.TryParse(startdate.Value, out dtStart))
            {
                Response.Write(CFun.AlertMeg(Resources.Site.DateFormatError, "back", ""));
                return;
            }

            if (!DateTime.TryParse(enddate.Value, out dtEnd))
            {
                Response.Write(CFun.AlertMeg(Resources.Site.DateFormatError, "back", ""));
                return;
            }

            if (dtEnd < dtStart)
            {
                Response.Write(CFun.AlertMeg(Resources.Site.DateFormatError, "back", ""));
                return;
            }

            int[] iWeek = new int[7];

            int iRoomCount = 0;

            foreach (ListItem item in cblRoom.Items)
            {
                if (item.Selected) iRoomCount++;
            }

            if (iRoomCount == 0)
            {
                Response.Write(CFun.AlertMeg(Resources.Site.NoRoomCodeSelected, "back", ""));
                return;
            }

            foreach (ListItem item in cblWeek.Items)
            {
                if (item.Selected) iWeek[int.Parse(item.Value)] = 1; else iWeek[int.Parse(item.Value)] = 0;
            }

            Hashtable hash = new Hashtable();

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                string v = ((TextBox)GridView1.Rows[i].Cells[1].FindControl("txtQuota")).Text;

                int n = 0;

                if (int.TryParse(v, out n))
                {
                    string k = GridView1.DataKeys[i][0].ToString();

                    hash[k] = v;
                }
            }

            //get data from page

            int iInventory = 0, iMin = 0, iMax = 0, iAdv = 0;

            int.TryParse(txtInventory.Text, out iInventory);

            int.TryParse(txtMin.Text, out iMin);
            int.TryParse(txtMax.Text, out iMax);
            int.TryParse(txtAdv.Text, out iAdv);


            string[] strRoomCodes = new string [iRoomCount];

            int iIndex = 0;

            foreach (ListItem item in cblRoom.Items)
            {
                if (item.Selected)
                {
                    strRoomCodes[iIndex] = item.Value;
                    iIndex++;

                }

            }

            Bll.AIR.AIR air = new EZCRS.HCMS.Bll.AIR.AIR();

            bool b = air.SaveRoomLevel(base.BasPropertyId, strRoomCodes, iInventory, iMin, iMax, iAdv, ddlStatus.SelectedValue, dtStart, dtEnd, iWeek, hash);

            if (b)
                Response.Write(CFun.AlertMeg(Resources.Site.SystemAddSuccess, "back", ""));
            else
                Response.Write(CFun.AlertMeg(Resources.Site.SystemError, "back", ""));
            

        }


        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ((Label)e.Row.Cells[0].FindControl("lblCode")).Text = GridView1.DataKeys[e.Row.RowIndex][0].ToString();

            }

        }
    }
}
